package com.sbpm.crmbpm.client.util;

import com.google.gwt.core.client.GWT;
import com.sbpm.crmbpm.client.deprecforms.FormFactory;
import com.sbpm.crmbpm.client.deprecforms.OnFinishCallBack;
import com.sbpm.crmbpm.client.deprecforms.ProcessForm;
import com.sbpm.crmbpm.client.deprecforms.controls.UsersComboBox;
import com.sbpm.crmbpm.client.services.ProcessService;
import com.sbpm.crmbpm.client.services.ProcessServiceAsync;
import com.sbpm.crmbpm.shared.data.TaskInfo;
import com.sencha.gxt.widget.core.client.grid.Grid;

import java.util.Map;

/**
 * Created by TT on 09.11.2014.
 */
public class TaskProcessor
{
    public static final String STATUS_READY = "Ready";
    public static final String STATUS_RESERVED = "Reserved";
    public static final String STATUS_IN_PROGRESS = "InProgress";
    private final ProcessServiceAsync processService = GWT.create( ProcessService.class );

    private FormFactory formFactory = new FormFactory();

    private UsersComboBox usersComboBox;
    private com.sencha.gxt.widget.core.client.grid.Grid taskGrid;
    private OnFinishCallBack onFinishCallBack;

    public TaskProcessor( UsersComboBox usersComboBox, Grid taskGrid, OnFinishCallBack onFinishCallBack )
    {
        this.usersComboBox = usersComboBox;
        this.taskGrid = taskGrid;
        this.onFinishCallBack = onFinishCallBack;
    }

    public void startTask()
    {
        if( taskGrid.getSelectionModel().getSelectedItem() != null && usersComboBox.getSelectedIndex() > -1 )
        {

            TaskInfo task = ( TaskInfo ) taskGrid.getSelectionModel().getSelectedItem();

            final Long taskId = task.getId();
            String status = task.getStatus();
            String user = usersComboBox.getValue( usersComboBox.getSelectedIndex() );
            startTask( taskId, status, user );

        }
    }

    public void startTask( final Long taskId, String status, String user )
    {
        if( STATUS_READY.equals( status ) || STATUS_RESERVED.equals( status ) )
        {

            processService.startTask( taskId, user, new SimpleAsyncCallback< Map< String, Object > >()
            {
                @Override
                public void onSuccess( Map< String, Object > result )
                {
                    openTaskForm( result, taskId );
                }
            } );
        }
        else if( STATUS_IN_PROGRESS.equals( status ) )
        {
            processService.getTaskData( taskId, new SimpleAsyncCallback< Map< String, Object > >()
            {
                @Override
                public void onSuccess( Map< String, Object > result )
                {
                    openTaskForm( result, taskId );
                }
            } );
        }
    }

    private void openTaskForm( Map< String, Object > result, Long taskId )
    {
        String taskName = ( ( TaskInfo ) taskGrid.getSelectionModel().getSelectedItem() ).getName();
        ProcessForm processForm = formFactory.createForm( taskName, taskId, usersComboBox.getValue( usersComboBox.getSelectedIndex() ) );
        processForm.setData( result );
        processForm.setOnFinishCallBack( onFinishCallBack );
        processForm.show();
    }

}
